﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Avatar.Model;
using AvatarHelper;
using System.Web.Security;
using Avatar.Company;
using Avatar.Common;
using Avatar.MembershipService;

public partial class Company_Favorite : System.Web.UI.Page
{
    public int _serviceID;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            var _user = LoginManager.GetCurrentUser();
            if (_user != null)
            {
                _serviceID = CompanyServiceManager.GetServiceType(_user.UserName).ServiceTypeID;
                using (var _adc = DBHelper.GetDataContext())
                {
                    var _rf = _adc.ResumeFavorites.Where(rf => rf.Company == _user.UserName);
                    if (_rf.Count() < 1) panel_op.Visible = false;
                    gv_ResumeList.DataSource = _rf;
                    gv_ResumeList.DataBind();

                }
            }
            panel_Content.Visible = false;
        }
    }
    protected string InterView(string _user, Guid _jobID)
    {
        string _strFav = "未通知";

        if (Membership.GetUser() != null)
        {
            var _company = Membership.GetUser().UserName;
            var _favorite = InterViewRecordManager.GetInterViewRecord(_company, _user, _jobID);
            if (_favorite != null) _strFav = "已通知";
        }

        return _strFav;
    }
    protected void cb_all_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox _sender = sender as CheckBox;
        var _check = _sender.Checked ? true : false;

        foreach (GridViewRow _row in gv_ResumeList.Rows)
        {
            foreach (Control _cell in _row.Controls)
            {
                foreach (Control _cb in _cell.Controls)
                {
                    if (_cb.GetType() == _sender.GetType())
                    {
                        ((CheckBox)_cb).Checked = _check;
                    }
                }
            }
        }
    }
    protected void btn_Interview_Click(object sender, EventArgs e)
    {
        panel_Content.Visible = true;
    }
    protected void btn_DeleteRecord_Click(object sender, EventArgs e)
    {
        try
        {
            bool _ok = false;

            foreach (GridViewRow _row in gv_ResumeList.Rows)
            {
                foreach (Control _cb in _row.Cells[0].Controls)
                {
                    if (_cb.GetType().ToString() == "System.Web.UI.WebControls.CheckBox")
                    {
                        if (((CheckBox)_cb).Checked)
                        {
                            if (Membership.GetUser() != null)
                            {
                                HiddenField _lbl_FavoriteRecordID = (HiddenField)_row.Cells[0].FindControl("lbl_FavoriteRecordID");
                                int _resumeID = _lbl_FavoriteRecordID != null ? Convert.ToInt32(_lbl_FavoriteRecordID.Value) : 0;

                                _ok = ResumeFavoritesManager.DeleteAFavorite(_resumeID);
                            }
                        }
                    }
                }
            }
            if (_ok)
                Helper.ShowMassage("取消收藏成功！", Request.Url.ToString());
        }
        catch (Exception _e)
        {
            lbl_Error.Text = "取消收藏出错:" + _e.Message;
        }
    }
    protected void btn_Ok_Click(object sender, EventArgs e)
    {
        try
        {
        bool _ok = false;
            int _i=0;
        foreach (GridViewRow _row in gv_ResumeList.Rows)
        {
            foreach (Control _cb in _row.Cells[0].Controls)
            {
                if (_cb.GetType().ToString() == "System.Web.UI.WebControls.CheckBox")
                {
                    if (((CheckBox)_cb).Checked)
                    {
                        _i++;
                        if (Membership.GetUser() != null)
                        {
                            var _company = Membership.GetUser().UserName;
                            HiddenField _lbl_ResumeID = (HiddenField)_row.Cells[0].FindControl("lbl_ResumeID");
                            string _resumeID = _lbl_ResumeID != null ? _lbl_ResumeID.Value : string.Empty;
                            HiddenField _lbl_JobID = (HiddenField)_row.Cells[0].FindControl("lbl_JobID");
                            Guid _jobID = _lbl_JobID != null ? new Guid(_lbl_JobID.Value) : Guid.Empty;

                            _ok= InterViewRecordManager.AddInterViewRecord(_company, _resumeID, _jobID, txt_Address.Text, txt_Content.Text );
                            
                        }
                    }
                }
            }
        }
        if (_ok)
            Helper.ShowMassage("发送面试通知成功！", Request.Url.ToString());
        if (_i == 0) throw new Exception("没有选择任何需要通知的简历");
        }
        catch (Exception _e)
        {
            lbl_Error.Text = "出错:" + _e.Message;
        }
    }
}
